package com.jinbei.restController;

import com.github.pagehelper.PageInfo;
import com.jinbei.common.Enum.ConstantsEnum;
import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.common.base.ResponseBase;
import com.jinbei.common.base.ResponseBaseEntity;
import com.jinbei.common.contants.Contants;
import com.jinbei.domain.Authority;
import com.jinbei.service.AuthorityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zflu
 * 授权 restful 接口
 * Created by lucky on 2018/7/9.
 */
@RestController
@RequestMapping("/author")
public class AuthorityController {

    private final Logger logger = LoggerFactory.getLogger(AuthorityController.class);

    @Autowired
    private AuthorityService authorityService;

    /**
     * @author zflu
     * @Description   权限集合查询
     * @date 2018/7/18 16:04
     * @param
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/queryAuthorityMap" ,logs = "权限集合查询")
    @GetMapping("/queryAuthorityMap")
    public ResponseBase queryAuthorityMap(){
        try {
            Map<String, Object> map = authorityService.queryAllAuthorUrl();
            return ResponseBaseEntity.setResultSuccess(map);
        }catch (Exception e){
            logger.error("查询授权集合失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description  根据权限id查询权限
     * @date 2018/7/9 15:41
     * @param id
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/queryAuthority" ,logs = "权限查询")
    @GetMapping("/queryAuthority")
    public ResponseBase queryAuthority(@RequestParam("id") int id){
        try {
            Authority authority = authorityService.queryAuthorityById(id);
            return ResponseBaseEntity.setResultSuccess(authority);
        }catch (Exception e){
            logger.error("查询权限详情失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   权限新增
     * @date 2018/7/9 15:40
     * @param authority
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/insertAuthority" ,logs = "权限新增")
    @PostMapping("/insertAuthority")
    public ResponseBase insertAuthority(@RequestBody Authority authority){
        try {
            int result = authorityService.insertAuthority(authority);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("权限新增失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   根据权限id删除权限
     * @date 2018/7/9 15:40
     * @param id
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/deleteAuthority" ,logs = "权限删除")
    @DeleteMapping("/deleteAuthority")
    public ResponseBase insertAuthority(@RequestParam("id") int id){
        try {
            int result = authorityService.deleteAuthorityById(id);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("删除权限失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description 权限修改
     * @date 2018/7/9 15:40
     * @param authority
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/updateAuthority" ,logs = "权限修改")
    @PutMapping("/updateAuthority")
    public ResponseBase updateAuthority(@RequestBody Authority authority){
        try {
            int result = authorityService.updateAuthority(authority);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("权限修改失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   授权分页查询
     * @date 2018/8/7 13:57
     * @param pageNum, pageSize
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/author/queryAuthorityByPage" ,logs = "授权分页查询")
    @GetMapping("/queryAuthorityByPage")
    public ResponseBase queryAuthorityByPage(@RequestParam("pageNum") int pageNum,
                                             @RequestParam("pageSize") int pageSize ){
        try {
            PageInfo pageInfo = authorityService.queryAuthorityPage(pageNum, pageSize);
            return ResponseBaseEntity.setResultSuccess(pageInfo);
        }catch (Exception e){
            logger.error("授权分页查询失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }
}
